package com.htc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.htc.model.Consumer;
import com.htc.model.InternalResponse;
import com.htc.model.Order;
import com.htc.model.OrderItem;
import com.htc.model.Product;
import com.htc.model.request.OrderRequest;
import com.htc.error.InternalError;
import com.htc.service.OrderService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@RestController
@RequestMapping("/order")
public class OrderController {
	@Autowired
	private OrderService orderService;

	@RequestMapping("queryListByConsumer")
	public InternalResponse queryListByConsumer(@SessionAttribute(value="consumerkey")Consumer consumer){
		InternalResponse internalResponse = null;
	    try{
	    	internalResponse = InternalResponse.success().withBody(orderService.findOrderByUid(consumer.getId()));
	    }catch(Exception e){
            e.printStackTrace();
            internalResponse = InternalResponse.fail();
	    }
	    return internalResponse;
	}
	
	@RequestMapping("queryList")
	public InternalResponse queryList(OrderRequest request){
		InternalResponse internalResponse = null;
	    try{
	    	internalResponse = InternalResponse.success().withBody(orderService.queryList(request));
	    }catch(Exception e){
            e.printStackTrace();
            internalResponse = InternalResponse.fail();
	    }
	    return internalResponse;
	}
	
	@RequestMapping("queryOrderProduct")
	public InternalResponse queryOrderProduct(int oid){
		InternalResponse internalResponse = null;
	    try{
	    	internalResponse = InternalResponse.success().withBody(orderService.queryOrderProduct(oid));
	    }catch(Exception e){
            e.printStackTrace();
            internalResponse = InternalResponse.fail();
	    }
	    return internalResponse;
	}
	
	@RequestMapping("queryListByCompany")
	public InternalResponse queryListByCompany(OrderRequest request,HttpServletRequest httpServletRequest){
		InternalResponse internalResponse = null;
	    try{
	    	Consumer consumer = (Consumer) httpServletRequest.getSession().getAttribute("consumerkey");
	    	if(consumer!=null) {
	    		request.setConsumer_id(consumer.getId());
	    	}
	    	internalResponse = InternalResponse.success().withBody(orderService.queryList(request));
	    }catch(Exception e){
            e.printStackTrace();
            internalResponse = InternalResponse.fail();
	    }
	    return internalResponse;
	}
	
	@RequestMapping("queryOrderDetail")
	public InternalResponse queryList(int oid){
		InternalResponse internalResponse = null;
	    try{
	    	internalResponse = InternalResponse.success().withBody(orderService.queryOrderListByOid(oid));
	    }catch(Exception e){
            e.printStackTrace();
            internalResponse = InternalResponse.fail();
	    }
	    return internalResponse;
	}
	
	@RequestMapping("queryOrderDetailPage")
	public InternalResponse queryOrderDetailPage(int oid){
		InternalResponse internalResponse = null;
	    try{
	    	internalResponse = InternalResponse.success().withBody(orderService.queryOrderDetail(oid));
	    }catch(Exception e){
            e.printStackTrace();
            internalResponse = InternalResponse.fail();
	    }
	    return internalResponse;
	}
	
	//���ɶ���ǰһ��
	@RequestMapping("placeOrderPrev")
	public InternalResponse save(String pids,String pNumbers,HttpSession session){
		InternalResponse internalResponse = null;
	    try{
	    	session.setAttribute("orderTemp", orderService.placeOrderPrev(pids, pNumbers));
	    	internalResponse = InternalResponse.success();
	    }catch(Exception e){
            e.printStackTrace();
            internalResponse = InternalResponse.fail();
	    }
	    return internalResponse;
	}
	
	@RequestMapping("getTempOrder")
	public InternalResponse getTempOrder(HttpSession session){
		InternalResponse internalResponse = null;
	    try{
	    	internalResponse = InternalResponse.success().withBody(session.getAttribute("orderTemp"));
	    }catch(Exception e){
            e.printStackTrace();
            internalResponse = InternalResponse.fail();
	    }
	    return internalResponse;
	}
	

	
	@RequestMapping("placeOrder")
	public InternalResponse save(Order order,@SessionAttribute(value="consumerkey")Consumer consumer,HttpSession session){
		InternalResponse internalResponse = null;
	    try{
	    	if(consumer!=null) {
	    		List<Product> products = (List<Product>) session.getAttribute("orderTemp");
	    		order.setUid(consumer.getId());
	    		orderService.placeOrder(order, products);
	    	}
	    	internalResponse = InternalResponse.success();
	    }catch(InternalError e){
            internalResponse = InternalResponse.fail().withBody(e.getErrorMsg());
	    }catch(Exception e){
            e.printStackTrace();
            internalResponse = InternalResponse.fail();
	    }
	    return internalResponse;
	}

	@RequestMapping("updateStatus")
	public InternalResponse del(String oid,int state){
		InternalResponse internalResponse = null;
	    try{
	    	orderService.updateOrderState(oid, state);
	    	internalResponse = InternalResponse.success();
	    }catch(InternalError e){
            internalResponse = InternalResponse.fail().withBody(e.getErrorMsg());
	    }catch(Exception e){
            e.printStackTrace();
            internalResponse = InternalResponse.fail();
	    }
	    return internalResponse;
	}
	
	
	@RequestMapping("updateComment")
	public void updateComment(OrderItem orderItem) {
		orderService.updateComment(orderItem);
	}

	@RequestMapping("queryCompany")
	public InternalResponse queryCompany(int consumerId) {
		return InternalResponse.success().withBody(orderService.queryCompany(consumerId));
	}


	@RequestMapping("queryProductComment")
	public InternalResponse queryProductComment(int pid){
		InternalResponse internalResponse = null;
	    try{
	    	internalResponse = InternalResponse.success().withBody(orderService.queryProductComment(pid));
	    }catch(Exception e){
            e.printStackTrace();
            internalResponse = InternalResponse.fail();
	    }
	    return internalResponse;
	}
	
}
